package com.szqhyxj.youxiangju.biz.register;

import android.text.TextUtils;

import com.szqhyxj.youxiangju.BuildConfig;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.RetrofitFacade;

import io.reactivex.Observable;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

class RegisterModel {

    static Observable<BaseResult> sms(RegisterEntity registerEntity) {
        return RetrofitFacade.Companion.getRetrofit()
                .create(RegisterService.class)
                .registerSms(
                        registerEntity.getAccount()
                );
    }

    static Observable<BaseResult> register(RegisterEntity registerEntity) {
        return RetrofitFacade.Companion.getRetrofit()
                .create(RegisterService.class)
                .register(
                        registerEntity.getAccount(),
                        registerEntity.getPassword(),
                        registerEntity.getVerificationCode(),
                        registerEntity.getReferralCode()
                );
    }

    public interface RegisterService {
        @FormUrlEncoded
        @POST("msg/sms/register")
        Observable<BaseResult> registerSms(@Field("phone") String phone);

        @FormUrlEncoded
        @POST("user/register")
        Observable<BaseResult> register(
                @Field("phone") String phone,
                @Field("loginPwd") String password,
                @Field("validCode") String verificationCode,
                @Field("invitePhone") String referralCode
        );
    }

    public static class RegisterEntity {
        private String account;
        private String password;
        private String ensurePassword;
        private String verificationCode;
        private String referralCode;
        private boolean agree;

        public boolean valid () {
            return !TextUtils.isEmpty(this.account)
                    && !TextUtils.isEmpty(this.password)
                    && !TextUtils.isEmpty(this.ensurePassword)
                    && !TextUtils.isEmpty(this.verificationCode)
                    && !TextUtils.isEmpty(this.referralCode)
                    && this.agree;
        }

        public String getAccount() {
            return account;
        }

        public void setAccount(String account) {
            this.account = account;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getEnsurePassword() {
            return ensurePassword;
        }

        public void setEnsurePassword(String ensurePassword) {
            this.ensurePassword = ensurePassword;
        }

        public String getVerificationCode() {
            return verificationCode;
        }

        public void setVerificationCode(String verificationCode) {
            this.verificationCode = verificationCode;
        }

        public String getReferralCode() {
            return referralCode;
        }

        public void setReferralCode(String referralCode) {
            this.referralCode = referralCode;
        }

        public boolean isAgree() {
            return agree;
        }

        public void setAgree(boolean agree) {
            this.agree = agree;
        }
    }
}